package com.hua.yuan.live.view.holder_view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.hua.yuan.common.BaseHolderView;
import com.hua.yuan.live.R;
import com.hua.yuan.live.listener.OnLiveClickListener;
import com.hua.yuan.live.repertory.cloud_data.res.RoomBean;

import java.util.List;

/**
 * 直播列表的ITEM
 */
public class HolderView_Live implements BaseHolderView<RoomBean>,View.OnClickListener{


    private TextView tv_live_tip;
    private TextView tv_live_title;
    private TextView tv_live_anchor_name;
    private TextView tv_live_anchor_level;
    private TextView tv_live_anchor_subtitle;
    private TextView tv_live_online_num;
    private TextView tv_live_online_time;
    private TextView tv_live_into_room;
    private TextView tv_live_intro_lookback;
    private ImageView iv_heardurl_live;
    private OnLiveClickListener mOnLiveClickListener;

    public HolderView_Live(View pView) {

        tv_live_tip = pView.findViewById(R.id.tv_live_tip);
        tv_live_title = pView.findViewById(R.id.tv_live_title);
        tv_live_anchor_name = pView.findViewById(R.id.tv_live_anchor_name);
        tv_live_anchor_level = pView.findViewById(R.id.tv_live_anchor_level);
        tv_live_anchor_subtitle = pView.findViewById(R.id.tv_live_anchor_subtitle);
        tv_live_online_num = pView.findViewById(R.id.tv_live_online_num);
        tv_live_online_time = pView.findViewById(R.id.tv_live_online_time);
        tv_live_into_room = pView.findViewById(R.id.tv_live_into_room);
        tv_live_intro_lookback = pView.findViewById(R.id.tv_live_intro_lookback);
        iv_heardurl_live=pView.findViewById(R.id.iv_heardurl_live);
        tv_live_into_room.setOnClickListener(this);
        tv_live_intro_lookback.setOnClickListener(this);
    }

    public void setOnLiveClickListener(OnLiveClickListener pOnLiveClickListener){

        this.mOnLiveClickListener=pOnLiveClickListener;
    }

    @SuppressLint({"StringFormatInvalid", "StringFormatMatches"})
    @Override
    public void bindHolder(@NonNull List<RoomBean> pList, int pPosition, @NonNull Context pContext) {

        if(pList==null || pList.size()==0 || (pPosition>pList.size())){
            return;
        }

        RoomBean roomBean=pList.get(pPosition);
        if(roomBean==null){
            return;
        }
        //主播昵称为空
        if(roomBean.getNickName() != null ){
            tv_live_anchor_name.setText(roomBean.getNickName());
        }

        Resources resource=pContext.getResources();
        if(roomBean.getIsLive()==0){
            //执行预告逻辑
            tv_live_title.setText(roomBean.getForeshow()==null?"":roomBean.getForeshow());
            tv_live_tip.setText(resource.getString(R.string.preview_live));
        }else{
            //执行直播逻辑
            tv_live_title.setText(roomBean.getTitle()==null?"":roomBean.getTitle());
            tv_live_tip.setText(resource.getString(R.string.living_live));

        }
        if(!TextUtils.isEmpty(roomBean.getHeadUrl())){
            Glide.with(pContext)
                    .load(roomBean.getHeadUrl())
                    .placeholder(R.mipmap.bg_default_liveing)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(iv_heardurl_live);
        }
        //没开播能不能显示在线人数
        tv_live_online_num.setText(String.format(resource.getString(R.string.online),roomBean.getViewNum()));
        tv_live_online_time.setText(String.format(resource.getString(R.string.living),roomBean.getTime()));
        tv_live_into_room.setTag(R.id.serach_live,pPosition);
        tv_live_intro_lookback.setTag(R.id.serach_live,pPosition);
//        if(roomBean.getIsLive()==0){
//
//        }else{
//
//        }
    }

    @Override
    public void onClick(View v) {

        int position= (int) v.getTag(R.id.serach_live);

        if(R.id.tv_live_into_room ==v.getId()){

            mOnLiveClickListener.gotoLiveRoom(position);

        }

        if(R.id.tv_live_intro_lookback ==v.getId()){
            mOnLiveClickListener.gotoLiveDetail(position);
        }
    }

}
